﻿function LoginModel() {
    var self = this;

    self.ReturnUrl;
}

LoginModel.prototype.Init = function (opts) {
    var self = this;

    var defaults = { /*returnUrl: "/"*/ };
    opts = $.extend({}, defaults, opts);

    self.ReturnUrl = opts.returnUrl;

    $("#dialog-login").dialog({
        modal: true,
        buttons: {
            "Login": function () {
                self.Login();
            },
            "Forgot Password": function () { }
        },
        dialogClass: "no-close",
        width: 400
    });
}

LoginModel.prototype.Login = function () {
    var self = this;

    var $form = $("#dialog-login form");

    if ($form.valid()) {
        var plainText = $("#Pwd").val();
        var shaObj = new jsSHA(plainText, "ASCII");
        var hash = shaObj.getHash("SHA-512", "HEX");

        var data = {
            uid: $("#Uid").val(),
            hashedPass: hash
        };

        $.ajax({
            url: "/Account/Login",
            data: JSON.stringify(data),
            type: 'POST',
            contentType: 'application/json',
            success: function (result) {
                if (result.Message == null || result.Message == "") {
                    window.location.href = self.ReturnUrl;
                }
                else {
                    alert(result.Message);
                }
            }
        });
    }
}